package Pachong;

import org.jsoup.Connection;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;
import com.huaban.analysis.jieba.JiebaSegmenter;
import org.junit.Test;

import java.io.*;
import java.util.List;
import java.util.Scanner;
import java.util.concurrent.Executor;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

/**
 * Create,d by 旧 on 2018/8/14.
 */
public class ZhiZhujing {
    String url="https://sou.zhaopin.com/jobs/searchresult.ashx?jl=%E5%8C%97%E4%BA%AC&kw=java&p=1&isadv=";
    Scanner input=new Scanner(System.in);
    ExecutorService excutorService= Executors.newWorkStealingPool(50);
    //数组
    public  static final String[] I_LIKE = {"ssm","springmvc","spring","mybatis","redis","quartz","javamail","spark","hadoop","hdfs","mapreduce","hive","mysql","oracle","hibernate","struts2","js","jquery","ajax","dubbo","zookeeper","ssh","servlet","jsp","jstl","echarts","tomcat","nginx","jsoup","kafka","db2","sqlserver"};
    File myfile=new File("java_招聘.csv");
    OutputStream outputStream=null;

    /* *
     * 多线程 读取50 页数据
     * */
    @Test
    public void readNums() throws FileNotFoundException {
        outputStream=new FileOutputStream(myfile);
        //第一个线程 1-10
        excutorService.execute(new Runnable() {
            @Override
            public void run() {
                for (int i = 1; i < 10; i++) {
                    System.out.println("正在爬行第"+i+"页 的数据");
                    readPage(url+i);
                }
            }
        });
        //第二个线程 11-20
        excutorService.execute(new Runnable() {
            @Override
            public void run() {
                for (int i = 11; i < 20; i++) {
                    System.out.println("正在爬行第"+i+"页 的数据");
                    readPage(url+i);
                }
            }
        });
        //第三个线程 21-30
        excutorService.execute(new Runnable() {
            @Override
            public void run() {
                for (int i = 21; i <30; i++) {
                    System.out.println("正在爬行第"+i+"页 的数据");
                    readPage(url+i);
                }
            }
        });
        input.next();//防止程序一闪而过
    }
    /**
     * 读取列表列
     * */
    public void readPage(String url){
        //1.
        Connection connect=Jsoup.connect(url);
        try {
            //2.将获取的连接地址 映射成一个Document 对象
            Document document=connect.get();
            //3.查找 DOM 对象中所有符合条件的  a 标签 节点-复位
            Elements elements=document.select("[style=font-weight: bold]");
            //4.遍历所有的标签的  href 属性值
            int i=1;
            for(Element element:elements){
                String href=element.attr("href");
                readDateilPage(href);
                //System.out.println(i+":"+href);
                i++;

            }
        } catch (IOException e) {
            e.printStackTrace();
        }


    }
    /**
     * 读取详情页
     */
    public void readDateilPage(String D_url){
        Connection connection=Jsoup.connect(D_url);
        try {
            Document document=connection.get();
            Elements select=document.select("[class=terminal-ul clearfix] li");
            String yuxin=select.get(0).select("strong").text();
            System.out.println("月薪:"+yuxin);
            String jingyan=select.get(4).select("strong").text();
            System.out.println("经验:"+jingyan);
            String xueli=select.get(5).select("strong").text();
            System.out.println("学历:"+xueli);
            //获取岗位要求
            Elements gwyg=document.select("[class=tab-inner-cont] div");
            String text=gwyg.get(0).select("span").text();
            text=text.toLowerCase();
            StringBuffer buffer=new StringBuffer();
            buffer.append(yuxin+","+jingyan+","+xueli);
            //结巴分词器来分词
            JiebaSegmenter jieba=new JiebaSegmenter();
            List<String> strings=jieba.sentenceProcess(text);
            for (String str:I_LIKE){
                if (strings.contains(str)){
                    buffer.append(str+",");
                }
            }
            //分完词之后的数据

            buffer.append("\n");
            outputStream.write(buffer.toString().getBytes());
            outputStream.flush();
            System.out.println();

        } catch (IOException e) {
            e.printStackTrace();
        }
        System.out.println("读取完毕！");
    }

}
